
$(function() {
    //首次打开页面时获取默认数据
    energyAnalysis($("#customerinfoid").val(), $("#startDate").val(), $("#endDate").val());
    //选择一个客户
    $("#companyName").dblclick(function() {
        $.baseCustomerApi({
            getBaseValue: function(customer) {
                $("#customerinfoid").val(customer.id);
                $("#companyCode").val(customer.companycode);
                $("#companyName").val(customer.customername);
            },
        });
    });

    //执行查询操作
    $("#searchEnergy").click(function() {
        energyAnalysis($("#customerinfoid").val(), $("#startDate").val(), $("#endDate").val());
    });

    function energyAnalysis(customerinfoid, startDate, endDate) {
        var param = {
            "customerinfoid": customerinfoid,
            "startDate": startDate,
            "endDate": endDate
        };
        $.ajax({
            contentType: "application/json",
            url: "findPowerEveryRegion.shtml",
            data: param,
            async: false,
            dataType: "json",
            success: function(data) {
                var dates = _.uniq(_.sortBy(_.pluck(data, 'statisticsdate')));
                var adddates = _.pluck(data, 'statisticsdate');
                 adddates[0] = '';
                var barseries = [];
                var series = [];
                _.each(data, function(item,i) {
                    if(item.power>0){
                    var array = [];
                    array[0] = item.areaname;
                    array[1] = item.power;
                    series[series.length] = array;
                        var serie = {};
                        serie.name = item.areaname;
                        var power = [];
                        power[0] = item.power;
                        serie.data = power;
                        barseries[barseries.length] = serie;
                    }
                });
                pie('container', '区域用电分析表', dates[0]+ '~' + dates[dates.length-1], '用电量', series,true);
                bar('container_second','区域用电分析表', dates[0]+ '~' + dates[dates.length-1],adddates,'用电量',barseries,'度');
            }
        });
    }
});


